package com.gf.framework.rediscachedemo.controller;

import com.gf.framework.redis.annotation.RepeatRequest;
import io.swagger.annotations.Api;
import lombok.Data;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * @author gaofei
 * @Description
 * @create 2022/5/24 13:50
 */
@RestController
@Api(tags = "分布式接口防止重入测试")
@RequestMapping("repeat/test")
public class RepeatTestController {

    // 最简单的重入注解，无参，自动根据方法名生成
    @RepeatRequest
    @GetMapping("noparam")
    public String noParamTest() throws InterruptedException {
        Thread.sleep(5000);
        return "test1";
    }

    // 指定键值防止重入
    @RepeatRequest(value = "valueparamtest")
    @GetMapping("valueparam")
    public String valueParamTest() throws InterruptedException {
        Thread.sleep(5000);
        return "valueparam";
    }

    // 用spel表达式注入key值
    @RepeatRequest(value = "spelkeytest", key = "#id")
    @GetMapping("spelkeytest")
    public String spelkeytest(@RequestParam("id")String id) throws InterruptedException {
        Thread.sleep(5000);
        return "spelkeytest";
    }

    @Data
    public static class MessageInfo {
        private String userName;
        private String message;
    }

    // 模仿登陆发短信，同一个用户一段时间内不可以重复发
    @RepeatRequest(value = "sendmessage", key = "#messageInfo.userName",
            refusetime = 1, timeUnit = TimeUnit.MINUTES, deleteKey = true)
    @PostMapping("sendmessage")
    public String sendmessage(@RequestBody MessageInfo messageInfo) throws InterruptedException {
        return messageInfo.toString();
    }
}
